/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.DccUser;
import me.zhengjie.exception.EntityExistException;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.DccUserRepository;
import me.zhengjie.modules.system.service.DccUserService;
import me.zhengjie.modules.system.service.dto.DccUserDto;
import me.zhengjie.modules.system.service.dto.DccUserQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.DccUserMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author cjl
* @date 2023-05-16
**/
@Service
@RequiredArgsConstructor
public class DccUserServiceImpl implements DccUserService {

    private final DccUserRepository dccUserRepository;
    private final DccUserMapper dccUserMapper;

    @Override
    public Map<String,Object> queryAll(DccUserQueryCriteria criteria, Pageable pageable){
        Page<DccUser> page = dccUserRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(dccUserMapper::toDto));
    }

    @Override
    public List<DccUserDto> queryAll(DccUserQueryCriteria criteria){
        return dccUserMapper.toDto(dccUserRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DccUserDto findById(Integer id) {
        DccUser dccUser = dccUserRepository.findById(id).orElseGet(DccUser::new);
        ValidationUtil.isNull(dccUser.getId(),"DccUser","id",id);
        return dccUserMapper.toDto(dccUser);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DccUserDto create(DccUser resources) {
        if(dccUserRepository.findByPhone(resources.getPhone()) != null){
            throw new EntityExistException(DccUser.class,"phone",resources.getPhone());
        }
        return dccUserMapper.toDto(dccUserRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DccUser resources) {
        DccUser dccUser = dccUserRepository.findById(resources.getId()).orElseGet(DccUser::new);
        ValidationUtil.isNull( dccUser.getId(),"DccUser","id",resources.getId());
        DccUser dccUser1 = null;
        dccUser1 = dccUserRepository.findByPhone(resources.getPhone());
        if(dccUser1 != null && !dccUser1.getId().equals(dccUser.getId())){
            throw new EntityExistException(DccUser.class,"phone",resources.getPhone());
        }
        dccUser.copy(resources);
        dccUserRepository.save(dccUser);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            dccUserRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<DccUserDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DccUserDto dccUser : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("openId", dccUser.getOpenId());
            map.put("身份", dccUser.getType());
            map.put("昵称", dccUser.getUserName());
            map.put("经度", dccUser.getLongitude());
            map.put("纬度", dccUser.getLatitude());
            map.put("所属街道", dccUser.getAddress());
            map.put("昵称", dccUser.getNickName());
            map.put("联系方式", dccUser.getPhone());
            map.put("邮箱", dccUser.getEmail());
            map.put("头像", dccUser.getAvatar());
            map.put("密码", dccUser.getPasswod());
            map.put("性别", dccUser.getSex());
            map.put("用户分组", dccUser.getGroupid());
            map.put("创建时间", dccUser.getCreatedAt());
            map.put("修改时间", dccUser.getUpdatedAt());
            map.put("身份", dccUser.getIdentity());
            map.put("操作者", dccUser.getOperator());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}